package com.ts.crawler.pipeline;

import org.apache.commons.codec.digest.DigestUtils;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;
import us.codecraft.webmagic.utils.FilePersistentBase;

import java.io.*;
import java.util.Map;

public class FlightInfoPipeline extends FilePersistentBase implements Pipeline {

    private final String path;
    public FlightInfoPipeline(String path){
        this.path = path;

    }

    public void process(ResultItems resultItems, Task task) {

        try{
            PrintWriter printWriter = new PrintWriter(new OutputStreamWriter(new FileOutputStream(this.getFile(path + DigestUtils.md5Hex(resultItems.getRequest().getUrl()) + ".json")), "UTF-8"));
            printWriter.println("{");
            int remain = resultItems.getAll().size();//记录剩余条目数
//            System.out.println(size);
            for (Map.Entry<String, Object> entry : resultItems.getAll().entrySet()) {

                StringBuilder sb = new StringBuilder();
                sb.append("\"" + entry.getKey() + "\":\t");

                if (entry.getValue() instanceof Map) {//key:result1 ,value:map
//                    Iterable value = (Iterable) entry.getValue();
//                    printWriter.println(entry.getKey() + ":");
//
//                    for (Object o : value) {
//                        printWriter.println(o);
//                    }
                    Map<String,String> map = (Map<String,String>)entry.getValue();

                    sb.append("{");
                    for(String key : map.keySet()){
                        sb.append("\"" + key + "\":\t\"" + map.get(key) + "\",");
                    }
                    sb.deleteCharAt(sb.lastIndexOf(","));
                } else {
                    sb.append("{\"").append(entry.getKey()).append("\":\t\"").append(entry.getValue()).append("\"}");
                }
                remain--;
                if(remain == 0){
                    sb.append("}");
                }else{
                    sb.append("},");
                }
                printWriter.println(sb);

            }
            printWriter.println("}");

            printWriter.close();
        }catch(IOException e){
            e.printStackTrace();
        }
    }
}
